//
//  EpointMainLoadTask.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-10.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointMainLoadTask.h"
#import "HttpUtil.h"
#import "GDataXMLNode.h"
#import "EpointStringUtil.h"
#import "EpointMainConfigModel.h"

@implementation EpointMainLoadTask

-(void)execute
{
    NSString *url = [[NSUserDefaults standardUserDefaults]objectForKey:Epoint_User_MainConfig_URL];
    NSString *bs = [HttpUtil getStr:url];
    
    NSMutableArray *dataMainArr = [EpointMainLoadTask getArray:bs default:@"1"];
    NSMutableArray *dataExtendArr = [EpointMainLoadTask getArray:bs default:@"2"];
    
    if (dataMainArr.count>0) {
        NSUserDefaults *u = [NSUserDefaults standardUserDefaults];
//        NSString *bak = [u objectForKey:Epoint_User_MainConfig];
//        if (![bak isEqualToString:bs]) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:dataMainArr forKey:@"Main"];
            [dic setObject:dataExtendArr forKey:@"Extend"];
            [u setObject:bs forKey:Epoint_User_MainConfig];
            [self sendDataRefreshUI:dic];
//        }
    }
}

+(NSMutableArray *)getArray:(NSString *)bs default:(NSString *)isdefault
{
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:bs  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"module"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        EpointMainConfigModel *m = [[EpointMainConfigModel alloc]init];
        
        GDataXMLElement *moduleID = [[arr elementsForName:@"moduleID"] objectAtIndex:0];
        m.moduleID = [moduleID stringValue];
        
        GDataXMLElement *type = [[arr elementsForName:@"type"] objectAtIndex:0];
        m.type = [type stringValue];
        
        GDataXMLElement *isDefault = [[arr elementsForName:@"isDefault"] objectAtIndex:0];
        m.isDefault = [isDefault stringValue];
        
        GDataXMLElement *modulename = [[arr elementsForName:@"modulename"] objectAtIndex:0];
        m.modulename = [modulename stringValue];
        
        GDataXMLElement *position = [[arr elementsForName:@"position"] objectAtIndex:0];
        m.position = [position stringValue];
        
        GDataXMLElement *platform = [[arr elementsForName:@"platform"] objectAtIndex:0];
        m.platform = [platform stringValue];
        
        GDataXMLElement *dyUrl = [[arr elementsForName:@"dyUrl"] objectAtIndex:0];
        m.dyUrl = [dyUrl stringValue];
        
        GDataXMLElement *paData = [[arr elementsForName:@"paData"] objectAtIndex:0];
        m.paData = [paData stringValue];
        
        GDataXMLElement *iconUrl = [[arr elementsForName:@"iconUrl"] objectAtIndex:0];
        m.iconUrl = [iconUrl stringValue];
        
        GDataXMLElement *apkUrl = [[arr elementsForName:@"apkUrl"] objectAtIndex:0];
        m.apkUrl = [apkUrl stringValue];
        
        GDataXMLElement *launchclass = [[arr elementsForName:@"launchclass"] objectAtIndex:0];
        m.launchclass = [launchclass stringValue];
        
        GDataXMLElement *packagename = [[arr elementsForName:@"packagename"] objectAtIndex:0];
        m.packagename = [packagename stringValue];
        
        GDataXMLElement *version = [[arr elementsForName:@"version"] objectAtIndex:0];
        m.version = [version stringValue];
        
        if ([m.isDefault isEqualToString:isdefault]) {
            if ([m.platform isEqualToString:@"1"]) {
                [listmp addObject:m];
            }
            else if ([m.platform isEqualToString:@"2"]&&!CurrentDeviceISiPad)
            {
                [listmp addObject:m];
            }
            else if([m.platform isEqualToString:@"3"]&&CurrentDeviceISiPad)
            {
                [listmp addObject:m];
            }
        }
    }
    return listmp;
}

@end
